package com.iemes.controller.qc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CorrelationDetection;
import com.iemes.entity.MeasurementTestForMap;
import com.iemes.entity.ProjectJcForMmap;
import com.iemes.entity.WarehouseFormMap;
import com.iemes.mapper.MaterialQualityMapper;
import com.iemes.mapper.MeasurementTestMapper;
import com.iemes.mapper.ProjuctJcMapper;
import com.iemes.mapper.WarehouseMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

//成品计数质检
@Controller
@RequestMapping("/wi/")
public class WarehouseController extends BaseController {
    @Inject
    private MeasurementTestMapper measurementTestMapper;

    @Inject
    private WarehouseMapper warehouseMapper;

    @Inject
    private ProjuctJcMapper projuctJcMapper;

    @Inject
    private MaterialQualityMapper materialQualityMapper;

    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/qc/wi/wi_list";
    }

    //获取分页数据
    @ResponseBody
    @RequestMapping("getWiByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort) throws Exception {


        String myValue = getPara("myValue");
        if(myValue == null || myValue.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            PageInfo pageInfo = new PageInfo<>(warehouseMapper.getAll());
            return pageInfo;

        }else {
            String[] split = myValue.split(",");
            if(split.length > 1){
                if(split[0].equals("name")){
                    //根据名称进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(warehouseMapper.getData(split[1],"name"));
                    return pageInfo;
                }else {
                    //根据编号进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(warehouseMapper.getData(split[1],"code"));
                    return pageInfo;
                }
            }else {
                PageInfo pageInfo = new PageInfo<>( warehouseMapper.getAll());
                return pageInfo;
            }
        }

    }

    //打开添加页面
    @RequestMapping("openAdd")
    public String openProcessManagementUI(){
        return Common.BACKGROUND_PATH + "/qc/wi/wi_add";
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addWi")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addOrder(){
        WarehouseFormMap warehouseFormMap = getFormMap(WarehouseFormMap.class);
        warehouseFormMap.get("detection_time");
        try {
            measurementTestMapper.addEntity(warehouseFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("editWiUI")
    public String openEditUi(Model model){
        String id = getPara("id");

        WarehouseFormMap warehouseFormMap = measurementTestMapper.findbyFrist("id", id, WarehouseFormMap.class);

        List<CorrelationDetection> detection = materialQualityMapper.getCd(warehouseFormMap.get("product_count_code").toString());

        List<CorrelationDetection> materialId = materialQualityMapper.getMaterialId(detection.get(0).get("detection_code").toString());

        model.addAttribute("wi",warehouseFormMap);

        model.addAttribute("cd",materialId);

        return Common.BACKGROUND_PATH + "/qc/wi/wi_edit";
    }


    //保存修改数据
    @ResponseBody
    @RequestMapping("editWi")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editUn(String txtGroupsSelect) throws Exception {
        WarehouseFormMap warehouseFormMap = getFormMap(WarehouseFormMap.class);
        warehouseMapper.deleteByAttribute("id", warehouseFormMap.get("id")+"", WarehouseFormMap.class);
        measurementTestMapper.addEntity(warehouseFormMap);
        return "success";
    }

    @ResponseBody
    @RequestMapping("delWi")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息删除")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delWi() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            baseMapper.deleteByAttribute("id", id, WarehouseFormMap.class);
        }
        return "success";
    }

    @RequestMapping("mqProduct")
    public String product(Model model){
        //获取相关的项目信息然之后分割字符串之后根据检验内容进行查询

        String id = getPara("id");

        //根据id获取数据
        List<WarehouseFormMap> ma = warehouseMapper.getId(id);
        //获取数据
        String name = ma.get(0).get("detection_pproduct").toString();
        //分割字符串
        String[] split = name.split(",");
        List<ProjectJcForMmap>  pjList = new ArrayList<>();
        if(split.length > 0){
            for (String n : split) {
                List<ProjectJcForMmap> content = projuctJcMapper.getContent(n);
                pjList.add(content.get(0));
            }
        }
        model.addAttribute("list",pjList);
        return Common.BACKGROUND_PATH + "/qc/mq/mq_product";
    }

}
